using System.Collections;
using UnityEngine;

public class UICard : MonoBehaviour
{
	public int indexInTeamData;

	public bool isUseUpdateThings;

	public bool isHideLevelAndRate;

	public CardType cardType;

	public UIButton btnUpgrate;

	public UISprite spriteCount;

	public UISprite spriteCountBg;

	public UISprite spriteCost;

	public UISprite spriteUpgradeTag;

	public UISprite spriteUpgradeEffect;

	public UISprite spriteBg;

	public UITexture textureLogo;

	public UILabel labelName;

	public UILabel labelCount;

	public UILabel labelLevel;

	public UILabel labelRate;

	public UILabel labelCost;

	public UILabel labelLevel_Text;

	public UILabel labelRate_Text;

	public GameObject ojLabelInfo;

	public GameObject ojBtnUpgradeThing;

	private string level;

	private bool isComLevel;

	private UISprite spriteTag;

	public bool isCombine;

	public int combineSelectCount;

	private void OnDestroy()
	{
		textureLogo.mainTexture = null;
		if (spriteTag != null)
		{
			UnityEngine.Object.Destroy(spriteTag);
			spriteTag = null;
		}
	}

	private void Start()
	{
		refreshInfomation();
		OnUnFocus();
		if (isComLevel)
		{
			labelLevel.text = level;
		}
		TweenScale component = spriteUpgradeTag.GetComponent<TweenScale>();
		component.enabled = true;
		component.from = Vector3.one;
		component.to = new Vector3(1f, 1.2f, 1f);
		component.duration = 0.5f;
		component.ResetToBeginning();
		component.PlayForward();
		if (ojLabelInfo.GetComponent<UILabel>() != null)
		{
			ojLabelInfo.GetComponent<UILabel>().color = Color.white;
		}
		if (labelCost.parent.GetComponent<UILabel>() != null)
		{
			labelCost.parent.GetComponent<UILabel>().color = Color.white;
		}
	}

	public void refreshInfomation()
	{
		textureLogo.mainTexture = (Resources.Load("logo/" + indexInTeamData, typeof(Texture)) as Texture);
		textureLogo.MakePixelPerfect();
		textureLogo.transform.localScale = Vector3.one * 0.4f;
		cardType = (CardType)(LoadData.teamDatas[indexInTeamData].data_type - 1);
		if (cardType == CardType.OFFENSIVE)
		{
			spriteBg.spriteName = "sprite_card_offesneBg";
		}
		else if (cardType == CardType.CONTROL)
		{
			spriteBg.spriteName = "sprite_card_controlBg";
		}
		else if (cardType == CardType.BALANCE)
		{
			spriteBg.spriteName = "sprite_card_balanceBg";
		}
		labelName.text = LoadData.teamDatas[indexInTeamData].data_name;
		labelRate.text = LoadData.teamDatas[indexInTeamData].data_rate;
		refeshCardCount();
	}

	private void OnClick()
	{
		if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().buildIndex != 6 || !GameData.IsFinishTutorial)
		{
			return;
		}
		Instance<UITeamManage>.instance.spriteHand.gameObject.SetActiveRecursively(state: false);
		if (Instance<UITeamManage>.instance.currentSelectTeamIndex != indexInTeamData)
		{
			OnFocus();
			return;
		}
		OnUnFocus();
		Instance<UITeamManage>.instance.spriteTag.gameObject.SetActiveRecursively(state: false);
		if (!Instance<UITeamManage>.instance.isInCombineProgress)
		{
			Instance<UITeamManage>.instance.currentSelectTeamIndex = -1;
		}
	}

	public void OnEnable()
	{
		if (!isUseUpdateThings)
		{
			btnUpgrate.gameObject.SetActiveRecursively(state: false);
			spriteUpgradeTag.gameObject.SetActiveRecursively(state: false);
			labelCount.gameObject.SetActiveRecursively(state: false);
			spriteCount.gameObject.SetActiveRecursively(state: false);
			spriteCountBg.gameObject.SetActiveRecursively(state: false);
		}
		if (isHideLevelAndRate)
		{
			labelLevel.gameObject.SetActiveRecursively(state: false);
			labelLevel_Text.gameObject.SetActiveRecursively(state: false);
			labelRate.gameObject.SetActiveRecursively(state: false);
			labelRate_Text.gameObject.SetActiveRecursively(state: false);
		}
		if (isComLevel)
		{
			labelLevel.text = level;
		}
	}

	public void OnClickUpgrate()
	{
		Instance<UITeamManage>.instance.showUpGrade();
		if (GameData.IsFinishTutorial)
		{
		}
	}

	public void OnFocus()
	{
		if (isUseUpdateThings)
		{
			if (!Instance<UITeamManage>.instance.isInCombineProgress)
			{
				spriteUpgradeTag.gameObject.SetActiveRecursively(state: false);
				Instance<UITeamManage>.instance.spriteTag.transform.parent = base.transform;
				Instance<UITeamManage>.instance.spriteTag.depth = base.transform.GetComponent<UISprite>().depth - 1;
				Instance<UITeamManage>.instance.spriteTag.gameObject.SetActiveRecursively(state: true);
				Instance<UITeamManage>.instance.spriteTag.transform.localPosition = new Vector3(0f, -17f, 0f);
				btnUpgrate.gameObject.SetActiveRecursively(state: true);
				if (isCanUpgrade() && indexInTeamData < 32)
				{
					labelCost.text = string.Empty + Global.card_upgrade_cost[GameData.TeamLevel[indexInTeamData] - 1];
					if (GameData.TeamLevel[indexInTeamData] >= 8)
					{
						spriteCost.spriteName = "sprite_coingroup_gem";
					}
					else
					{
						spriteCost.spriteName = "sprite_coingroup_coin";
					}
					ojLabelInfo.SetActiveRecursively(state: false);
					ojBtnUpgradeThing.SetActiveRecursively(state: true);
				}
				else
				{
					ojLabelInfo.SetActiveRecursively(state: true);
					ojBtnUpgradeThing.SetActiveRecursively(state: false);
				}
			}
			else
			{
				if ((!isCombine && Instance<UITeamManage>.instance.combineSelectCount >= Instance<UITeamManage>.instance.combineNeedConut) || GameData.CardsCount[indexInTeamData] == 0)
				{
					return;
				}
				if (spriteTag == null)
				{
					spriteTag = (UnityEngine.Object.Instantiate(Instance<UITeamManage>.instance.spriteTag) as UISprite);
					spriteTag.transform.parent = base.transform;
					spriteTag.transform.localScale = MathHelper.setY(Vector3.one, 0.85f);
					spriteTag.depth = base.transform.GetComponent<UISprite>().depth - 1;
					spriteTag.transform.localPosition = MathHelper.setY(Vector3.one, -3f);
				}
				if (!isCombine)
				{
					spriteTag.gameObject.SetActiveRecursively(state: true);
					isCombine = true;
					if (Instance<UITeamManage>.instance.combineSelectCount + GameData.CardsCount[indexInTeamData] <= Instance<UITeamManage>.instance.combineNeedConut)
					{
						combineSelectCount = GameData.CardsCount[indexInTeamData];
					}
					else
					{
						combineSelectCount = Instance<UITeamManage>.instance.combineNeedConut - Instance<UITeamManage>.instance.combineSelectCount;
					}
					Instance<UITeamManage>.instance.combineSelectCount += combineSelectCount;
					Instance<UITeamManage>.instance.labelCombineCount.text = Instance<UITeamManage>.instance.combineSelectCount + "/" + Instance<UITeamManage>.instance.combineNeedConut;
					if (Instance<UITeamManage>.instance.combineSelectCount >= Instance<UITeamManage>.instance.combineNeedConut)
					{
						Instance<UITeamManage>.instance.spriteRightBtnBg.gameObject.SetActiveRecursively(state: true);
						Instance<UITeamManage>.instance.btnUpgrade.gameObject.SetActiveRecursively(state: true);
					}
				}
				else
				{
					spriteTag.gameObject.SetActiveRecursively(state: false);
					isCombine = false;
					Instance<UITeamManage>.instance.combineSelectCount -= combineSelectCount;
					combineSelectCount = 0;
					Instance<UITeamManage>.instance.labelCombineCount.text = Instance<UITeamManage>.instance.combineSelectCount + "/" + Instance<UITeamManage>.instance.combineNeedConut;
					Instance<UITeamManage>.instance.spriteRightBtnBg.gameObject.SetActiveRecursively(state: false);
					Instance<UITeamManage>.instance.btnUpgrade.gameObject.SetActiveRecursively(state: false);
				}
			}
		}
		else
		{
			btnUpgrate.gameObject.SetActiveRecursively(state: false);
		}
		if (Instance<UITeamManage>.instance.currentSelectTeamIndex >= 0 && Instance<UITeamManage>.instance.currentSelectTeamIndex != indexInTeamData)
		{
			for (int i = 0; i < Instance<UITeamManage>.instance.cards.Count; i++)
			{
				if (Instance<UITeamManage>.instance.cards[i].indexInTeamData == Instance<UITeamManage>.instance.currentSelectTeamIndex)
				{
					Instance<UITeamManage>.instance.cards[i].OnUnFocus();
					break;
				}
			}
		}
		if (!Instance<UITeamManage>.instance.isInCombineProgress)
		{
			Instance<UITeamManage>.instance.currentSelectTeamIndex = indexInTeamData;
		}
	}

	public void OnUnFocus()
	{
		btnUpgrate.gameObject.SetActiveRecursively(state: false);
		refeshCardCount();
	}

	public void refeshCardCount()
	{
		if (spriteTag != null)
		{
			spriteTag.gameObject.SetActiveRecursively(state: false);
		}
		if (isCanUpgrade() && isUseUpdateThings)
		{
			spriteUpgradeEffect.gameObject.SetActiveRecursively(state: true);
		}
		else
		{
			spriteUpgradeEffect.gameObject.SetActiveRecursively(state: false);
		}
		if (indexInTeamData >= 32)
		{
			return;
		}
		labelLevel.text = string.Empty + GameData.TeamLevel[indexInTeamData];
		if (GameData.TeamLevel[indexInTeamData] - 1 != 9)
		{
			labelCount.text = GameData.CardsCount[indexInTeamData] + "/" + Global.card_upgrade_total[GameData.TeamLevel[indexInTeamData] - 1];
		}
		else
		{
			labelCount.text = "MAX";
		}
		if (isUseUpdateThings)
		{
			if (isCanUpgrade())
			{
				spriteUpgradeTag.gameObject.SetActiveRecursively(state: true);
			}
			else
			{
				spriteUpgradeTag.gameObject.SetActiveRecursively(state: false);
			}
		}
		else
		{
			spriteUpgradeTag.gameObject.SetActiveRecursively(state: false);
		}
		float num = 102f;
		if (GameData.TeamLevel[indexInTeamData] - 1 != 9)
		{
			num = 102f * ((float)GameData.CardsCount[indexInTeamData] / (float)Global.card_upgrade_total[GameData.TeamLevel[indexInTeamData] - 1]);
		}
		num = (int)((!(num > 102f)) ? num : 102f);
		spriteCount.SetDimensions((int)num, 12);
		if (spriteCount.width < 10)
		{
			spriteCount.transform.localScale = MathHelper.setX(spriteCount.transform.localScale, spriteCount.width / 10);
		}
		else
		{
			spriteCount.transform.localScale = Vector3.one;
		}
		if (isComLevel)
		{
			labelLevel.text = level;
		}
	}

	public void playReduceAnimation()
	{
		float num = 102f;
		if (GameData.TeamLevel[indexInTeamData] - 1 != 9)
		{
			num = 102f * ((float)GameData.CardsCount[indexInTeamData] / (float)Global.card_upgrade_total[GameData.TeamLevel[indexInTeamData] - 1]);
		}
		num = (int)((!(num > 102f)) ? num : 102f);
		StartCoroutine(ChangeLength((int)num));
	}

	private IEnumerator ChangeLength(int targetWidth)
	{
		yield return new WaitForEndOfFrame();
		spriteCount.transform.localScale = Vector3.one;
		spriteCount.SetDimensions(102, 12);
		while (true)
		{
			yield return new WaitForSeconds(0.01f);
			if (spriteCount.width > targetWidth)
			{
				spriteCount.width--;
				if (spriteCount.width == 10 && targetWidth < 10)
				{
					break;
				}
			}
		}
		spriteCount.transform.localScale = MathHelper.setX(spriteCount.transform.localScale, targetWidth / 10);
	}

	public void changeLevel(int _level)
	{
		isComLevel = true;
		level = string.Empty + _level;
	}

	public bool isCanUpgrade()
	{
		if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().buildIndex != 6)
		{
			return false;
		}
		if (indexInTeamData < 32 && GameData.TeamLevel[indexInTeamData] < 10 && GameData.CardsCount[indexInTeamData] >= Global.card_upgrade_total[GameData.TeamLevel[indexInTeamData] - 1])
		{
			return true;
		}
		return false;
	}

	public int getRate()
	{
		if (MathHelper.isArrayHaveNum(GameHelper.getRateDTeam(), indexInTeamData))
		{
			return 0;
		}
		if (MathHelper.isArrayHaveNum(GameHelper.getRateCTeam(), indexInTeamData))
		{
			return 1;
		}
		if (MathHelper.isArrayHaveNum(GameHelper.getRateBTeam(), indexInTeamData))
		{
			return 2;
		}
		if (MathHelper.isArrayHaveNum(GameHelper.getRateATeam(), indexInTeamData))
		{
			return 3;
		}
		if (MathHelper.isArrayHaveNum(GameHelper.getRateSTeam(), indexInTeamData))
		{
			return 4;
		}
		return 0;
	}
}
